// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
// This source file is part of the Cangjie project, licensed under Apache-2.0
// with Runtime Library Exception.
//
// See https://cangjie-lang.cn/pages/LICENSE for license information.

#include <iostream>

#include "Platform/System.h"
#include "Platform/EnvironmentVariable.h"

#include "Path.h"

#include "Config.h"

using namespace CangjieMigrator;
using namespace Platform;

const Path Config::ExecutablePath = System::GetExecutablePath();
const Path Config::ExecutableDir = ExecutablePath.parent_path();
Path Config::CangjieHome;

Path Config::GetBinDependencyDir(const std::string &version)
{
    return ExecutableDir / "versions" / version;
}

bool Config::PrepareEnvironment()
{
    static const std::string CANGJIE_HOME_VARIABLE_NAME = "CANGJIE_HOME";

    const auto cangjieHome = EnvironmentVariable::GetPath(CANGJIE_HOME_VARIABLE_NAME);
    if (!cangjieHome) {
        std::cerr << "The environment variable " << CANGJIE_HOME_VARIABLE_NAME << " cannot be found." << std::endl;
        std::cerr << "Please run the envsetup script in version 0.53.18 canjie-sdk." << std::endl;
        return false;
    }

    if (!is_directory(*cangjieHome)) {
        std::cerr << "The directory pointed to by environment variable " << CANGJIE_HOME_VARIABLE_NAME
                  << " does not exist." << std::endl;
        return false;
    }

    CangjieHome = *cangjieHome;
    return true;
}
